﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HealthManagement.Model;
using HealthManagement.Data.Service;
using HealthManagement.Data.Model;
using Telerik.Windows.Controls;
using PM.Controls.Common;

namespace HealthManagement.View
{
    /// <summary>
    /// DoctorPanel.xaml 的交互逻辑
    /// </summary>
    public partial class DoctorPanel : Page
    {
        UserService ser = new UserService();
        IList<UserModel> users;
        public DoctorPanel()
        {
           
            InitializeComponent();

            var list = ser.LoadDoctorUserList();


            InitUsersData(list);
        }

        private void InitUsersData(IList<DoctorUser> list)
        {
            users = new List<UserModel>();
            int i = 0;
            foreach (var user in list)
            {
                users.Add(new UserModel()
                {
                    UserId = user.userId,
                    Name = user.username,
                    Photo = "/Images/EmpID" + ++i + ".jpg",
                    Mobile = user.MobilePhoneNumber,
                    Address = user.Address,
                   
                    Email = user.email,
                    IdCard = user.IdCard

                });
            }
            userList.ItemsSource = users;
        }


        //选择所有用户
        private void RadButtonAll_Click(object sender, RoutedEventArgs e)
        {
            var list = ser.LoadDoctorUserList();

            InitUsersData(list);
        }

        //按名字的首字母进行选择
        private void Letter_Click(object sender, RoutedEventArgs e)
        {
            string key = (sender as RadButton).Content.ToString().ToLower();
            var list = ser.LoadDoctorUserList(c => c.username.ToLower().StartsWith(key));

            InitUsersData(list);
        }

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            DoctorForm win = new DoctorForm();
            win.onSaveHandler += () =>
            {
                var list = ser.LoadDoctorUserList();
                InitUsersData(list);
            };
            win.ShowDialog();
        }

        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
            DoctorForm win = new DoctorForm((sender as RadButton).Tag.ToString());
            win.onSaveHandler += () =>
            {
                var list = ser.LoadDoctorUserList();
                InitUsersData(list);
            };
            win.ShowDialog();
        }

        string userId;
        private void DelButton_Click(object sender, RoutedEventArgs e)
        {
            DoctorUser loginUser = Application.Current.Properties["loginUser"] as DoctorUser;
            if(loginUser.IdCard != "0001")
            {
                Tools.ShowMessage(true, "没有删除权限，不能执行删除操作！");
                return;
            }

            var ii = (((sender as RadButton).Parent as StackPanel).Parent as StackPanel).Children[0];
            string idCard = ((ii as StackPanel).Children[1] as TextBlock).Text;

            if (idCard == "0001")
            {
                Tools.ShowMessage(true, "该用户不能被删除！");
                return;
            }

            userId = (sender as RadButton).Tag.ToString();
            RadWindow.Confirm("是否要删除该用户？", new EventHandler<WindowClosedEventArgs>(OnConfirmClosed));
                     
        }

        private void OnConfirmClosed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                if (!string.IsNullOrEmpty(userId))
                {
                    UserService ser = new UserService();
                    var user = ser.Get("userId", userId);
                    ser.Delete(user, "userId", "userId");

                    this.RadButtonAll_Click(null, null);
                }
            }
        }

        private void searchTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            string key = (sender as RadWatermarkTextBox).Text.Trim().ToLower();

            if (key.Length > 0)
            {
                var list = ser.LoadDoctorUserList(c => c.username.ToLower().Contains(key));

                InitUsersData(list);
            }
        }


        

    }
}
